package main.util;

import java.util.HashMap;
import java.util.Map;

public class ReplaceSQLString {
	
	String colName="MENUURL";
	
	String abbreviation="ABBREVIATION";
	
	Map<String,String> replaceURL;
	
	public ReplaceSQLString() {
		super();
		replaceURL=new HashMap<String, String>();
		/*replaceURL.put("201", "http://ngbusi-gx.cs.cmos");
		replaceURL.put("202", "http://nganoce.cs.cmos");
		replaceURL.put("203", "http://ngwf-gx.cs.cmos");
		replaceURL.put("204", "http://ngwf-gx.cs.cmos");
		replaceURL.put("205", "http://ngwf.cs.cmos");
		
		replaceURL.put("206", "http://ngmm.cs.cmos");
		replaceURL.put("207", "http://ngcct.cs.cmos");
		replaceURL.put("209", "http://ngcslog.cs.cmos");
		replaceURL.put("212", "http://ngba.cs.cmos");*/
		
		replaceURL.put("201", "http://[FrameServerHost]");
		replaceURL.put("202", "http://[FrameServerHost]");
		replaceURL.put("203", "http://[FrameServerHost]");
		replaceURL.put("204", "http://[FrameServerHost]");
		replaceURL.put("205", "http://[FrameServerHost]");
		
		replaceURL.put("206", "http://[FrameServerHost]");
		replaceURL.put("207", "http://[FrameServerHost]");
		replaceURL.put("209", "http://[FrameServerHost]");
		replaceURL.put("212", "http://[FrameServerHost]");
		
		replaceURL.put("101", "http://[FrameServerHost]");
		replaceURL.put("108", "http://[FrameServerHost]");
	}
	
	//替换链接
	public String getReplaceURLStr(String sql){
		if(sql.indexOf(colName)<0){
			return sql;
		}
		
		String moduleId=this.getColNameValue(sql, "MODULEID");
		
		String url=replaceURL.get(moduleId);
		if(url==null){
			return sql;
		}
		
		String originUrl=this.getColNameValue(sql, colName);
		
		int lastIndex=originUrl.indexOf("//")+2;
		if(lastIndex<2){
			return sql;
		}
		lastIndex=originUrl.indexOf("/", lastIndex);
		originUrl=originUrl.substring(0, lastIndex);
		
		System.out.println("需要替换的链接："+originUrl+"   替换       "+url);
				
		return sql.replace(originUrl, url);
	}

	private int getSpan(String[] sqls,String colName){
		
		int span=(sqls.length-1)/2;
		int location=-1;
		
		
		for(int i=0;i<sqls.length;i++){
			if(sqls[i].contains(colName)){
				location=i+span;
				break;
			}
		}
		
		return location;
	}
	
	//获取colname的值
	public String getColNameValue(String sql,String colName){
		if(sql.indexOf(colName)<0){
			return "";
		}
		
		String[] sqls=sql.split(",");
		String value=sqls[this.getSpan(sqls,colName)];
		
		int firstIndex=value.indexOf("'")+1;
		int laxtIndex=value.indexOf("'", firstIndex);
		
		return value.substring(firstIndex, laxtIndex);
	}
	
	/*sql里面添加字段和值*/
	public String getSQLAddCol(String sql,String addColName,String colValue){
		if(sql.indexOf(addColName)>0){
			return sql;
		}
		
		String[] sqls=sql.split(",");
		
		int begin=(sqls.length-1)/2;
		int last=sqls.length-1;
		
		StringBuffer sb=new StringBuffer();
		
		for(int i=0;i<sqls.length;i++){
			if(i==begin){
				sb.append(addColName);
				sb.append(",");
			}
			if(i==last){
				sb.append("'"+colValue+"',");
			}
			sb.append(sqls[i]);
			sb.append(",");
		}
		
		sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}
	
	private String getValueProfix(String value){
		if(value.indexOf("'")<2){
			return "";
		}
		
		return value.substring(0,value.indexOf("'"));
	}
	
	private String getValueSufix(String value){
		if(value.lastIndexOf("'")>value.length()-2){
			return "";
		}
		
		return value.substring(value.lastIndexOf("'")+1,value.length());
	}
	
	/*sql里面替换字段和值*/
	public String getSQLReplaceCol(String sql,String RColName,String colValue){
		if(sql.indexOf(RColName)<0){
			return sql;
		}
		
		String[] sqls=sql.split(",");
		int last=this.getSpan(sqls, RColName);
		sqls[last]=getValueProfix(sqls[last])+"'"+colValue+"'"+getValueSufix(sqls[last]);
		
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<sqls.length;i++){
			sb.append(sqls[i]);
			sb.append(",");
		}
		
		sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}
	
	public static void main(String[] args) {
		ReplaceSQLString rss=new ReplaceSQLString();
		String sql="MENUURL,MENUID,MODULEID,PARENTID,IMAGEURL,MENUNAME,MENUDESCRIPTION,DISPLAYNO,OPENMODULE,AUTHID,CLICKTOTAL,ABBREVIATION) values ('https://[FrameServerHost]/ngwf_gx/src/module/sr/handle/myWorkSheet.html','000204003001','204','000204003','','我的工单','工单处理工作台','1','N','000204003001','0','wdgd');";
		
		System.out.println(rss.getReplaceURLStr(sql));
		
	}

}
